package com.orcs.leefei.utils.kotlin

/**
 * Created by FeiLee on 2016/12/26 11:16.
 */

import android.app.Activity
import android.support.v4.view.ViewCompat
import android.view.View
import android.support.v4.app.Fragment as SupportFragment

inline fun View?.visible() {
    if (this != null && visibility != View.VISIBLE) {
        visibility = View.VISIBLE
    }
}

inline fun View?.gone() {
    if (this != null && visibility != View.GONE) {
        visibility = View.GONE
    }
}

inline fun View?.invisible() {
    if (this != null && visibility != View.INVISIBLE) {
        visibility = View.INVISIBLE
    }
}

inline fun View.sharedElementName(transitionName: String) {
    ViewCompat.setTransitionName(this, transitionName)
}

inline fun <reified T : View> Activity.rootFind(id: Int): T =
        window.decorView.findViewById(id) as T

inline fun <reified T : View> SupportFragment.rootFind(id: Int): T =
        activity.window.decorView.findViewById(id) as T

inline fun <reified T : View> Activity.rootFindOptional(id: Int): T? = window.decorView?.findViewById(id) as? T
inline fun <reified T : View> SupportFragment.rootFindOptional(id: Int): T? = activity.window.decorView?.findViewById(id) as? T

//fun <T : View> View.findView(id: Int): T? = findViewById(id) as? T
//fun View.show(): Unit {
//    visibility = View.VISIBLE
//}
//fun View.hide(): Unit {
//    visibility = View.GONE
//}
//
//fun View.toggleVisibility(): Unit = if (visibility == View.VISIBLE) hide() else show()
//
//fun <k : View> Activity.viewById(id: Int): k {
//    val view = findViewById(id) ?: throw IllegalArgumentException("Given ID could not be found in current layout!")
//    return view as k
//}
//
//fun <k : View> Fragment.viewById(id: Int): k {
//    val view = view.findViewById(id) ?:
//            throw IllegalArgumentException("Given ID could not be found in current layout!")
//    return view as k
//}
//
//fun <k : View> SupportFragment.viewById(id: Int): k {
//    val view = view?.findViewById(id) ?:
//            throw IllegalArgumentException("Given ID could not be found in current layout!")
//    return view as k
//}
//
//fun <k : View> Activity.optionalViewById(id: Int): k? {
//    val view = findViewById(id) ?: return null
//    return view as? k
//}
//
//fun <k : View> Fragment.optionalViewById(id: Int): k? {
//    val view = view?.findViewById(id) ?: return null
//    return view as? k
//}
//
//fun <k : View> SupportFragment.optionalViewById(id: Int): k? {
//    val view = view?.findViewById(id) ?: return null
//    return view as? k
//}